﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SCS.DAL;
using SCS.Common.Models;
using System.Linq.Expressions;

namespace SCS.Business
{
    public class TermServices
    {
        IUnitOfWork _unitOfWork;
        public TermServices()
        {
            _unitOfWork = new UnitOfWork();
        }

        /// <summary>
        /// AddTerm
        /// </summary>
        /// <param name="term">term</param>
        public void AddTerm(Term term)
        {
            int id = _unitOfWork.Term.GetAll().Max(t => (int?)t.termID) ?? 0;
            term.termID = id + 1;
            _unitOfWork.Term.Add(term);
            _unitOfWork.Commit();
        }

        /// <summary>
        /// CheckTermName
        /// </summary>
        /// <param name="term">Term</param>
        /// <returns>bool</returns>
        public bool CheckTermName(Term term)
        {
            Expression<Func<Term, bool>> predicate = (p => p.termName == term.termName && p.SchoolYear.schoolYearID == term.schoolYearID);
            int count = _unitOfWork.Term.Get(predicate).ToList<Term>().Count;

            if (count == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// GetAll term
        /// </summary>
        /// <returns>term list</returns>
        public List<Term> GetAll()
        {
            return _unitOfWork.Term.GetAll().ToList<Term>();
        }

        /// <summary>
        /// GetCount
        /// </summary>
        /// <returns>count</returns>
        public int GetCount()
        {
            return 1;
        }

        /// <summary>
        /// GetAllSchoolYear
        /// </summary>
        /// <returns>school year list</returns>
        public IEnumerable<SchoolYear> GetAllSchoolYear()
        {
            return _unitOfWork.SchoolYear.GetAll().OrderByDescending(m => m.schoolYearID);
        }
    }
}
